package com.jrauto.CarAppBackend.controller;

/**
 * @author : huiMing
 * Date : 2025年06月27日 10:59
 * @version V1.0
 */

import com.jrauto.CarAppBackend.model.Series;
import com.jrauto.CarAppBackend.service.SeriesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车系管理 RESTful API 控制器
 */
@Tag(name = "SeriesController", description = "汽车车系管理")
@RestController
@RequestMapping("/series")
public class SeriesController {

    @Resource
    private SeriesService seriesService;

    /**
     * 新增车系
     * POST /api/series
     * @param series 车系实体对象
     * @return ResponseEntity 包含操作结果
     */
    @Operation(summary = "新增汽车车系", description = "提交一个新的汽车车系记录")
    @PostMapping("/add")
    public ResponseEntity<String> addSeries(@RequestBody Series series) {
        try {
            boolean success = seriesService.addSeries(series);
            if (success) {
                return ResponseEntity.status(HttpStatus.CREATED).body("车系添加成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("车系添加失败");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取车系详情
     * GET /api/series/{id}
     * @param id 车系ID
     * @return ResponseEntity 包含车系实体或错误信息
     */
    @Operation(summary = "根据ID获取车系详情", description = "根据车系ID查询单条车系记录")
    @GetMapping("/{id}")
    public ResponseEntity<Series> getSeriesById(@PathVariable Long id) {
        try {
            Series series = seriesService.getSeriesById(id);
            if (series != null) {
                return ResponseEntity.ok(series);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 获取所有车系列表
     * GET /api/series
     * @return ResponseEntity 包含车系列表或错误信息
     */
    @Operation(summary = "获取所有车系列表", description = "查询所有汽车车系记录")
    @GetMapping
    public ResponseEntity<List<Series>> getAllSeries() {
        try {
            List<Series> seriesList = seriesService.getAllSeries();
            return ResponseEntity.ok(seriesList);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 根据品牌ID获取车系列表
     * GET /api/series/brand/{brandId}
     * @param brandId 品牌ID
     * @return ResponseEntity 包含车系列表或错误信息
     */
    @Operation(summary = "根据品牌ID获取车系列表", description = "根据所属品牌ID查询车系记录")
    @GetMapping("/id/{brandId}")
    public ResponseEntity<List<Series>> getSeriesByBrandId(@PathVariable Long brandId) {
        try {
            List<Series> seriesList = seriesService.getSeriesByBrandId(brandId);
            return ResponseEntity.ok(seriesList);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }


    /**
     * 根据车系英名称获取车系列表
     * GET /api/series/name/{name}
     * @param brand 车系英文名
     * @return ResponseEntity 包含车系列表或错误信息
     */
    @Operation(summary = "根据名称获取车系列表", description = "根据车系中文名查询车系记录")
    @GetMapping("/brand/{brand}")
    public ResponseEntity<List<Series>> getSeriesByBrand(@PathVariable String brand) {
        try {
            List<Series> seriesList = seriesService.getSeriesByBrand(brand);
            return ResponseEntity.ok(seriesList);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 更新车系信息
     * PUT /api/series/{id}
     * @param id 车系ID
     * @param series 更新后的车系实体
     * @return ResponseEntity 包含操作结果
     */
    @Operation(summary = "更新车系信息", description = "更新指定ID的汽车车系信息")
    @PutMapping("/update/{id}")
    public ResponseEntity<String> updateSeries(@PathVariable Integer id, @RequestBody Series series) {
        try {
            series.setId(id);
            boolean success = seriesService.updateSeries(series);
            if (success) {
                return ResponseEntity.ok("车系更新成功");
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("车系更新失败，可能ID不存在或数据异常");
            }
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误：" + e.getMessage());
        }
    }

    /**
     * 删除车系
     * DELETE /api/series/{id}
     * @param id 车系ID
     * @return ResponseEntity 包含操作结果
     */
    @Operation(summary = "删除车系", description = "根据ID删除一个汽车车系")
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteSeries(@PathVariable Long id) {
        try {
            boolean success = seriesService.deleteSeries(id);
            if (success) {
                return ResponseEntity.ok("车系删除成功");
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("车系删除失败，车系不存在");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误：" + e.getMessage());
        }
    }
}
